package handler

import (
	"github.com/gin-gonic/gin"
	"scenic-spot-sys/model"
)

func ScenicAdd(c *gin.Context) {
	name := c.PostForm("name")
	address := c.PostForm("address")
	price := c.PostForm("price")
	if name == "" || address == "" || price == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Scenic{
		Name:    name,
		Address: address,
		Price:   price,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
	})
	return
}
func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不存在",
		})
		return
	}
	if file.Size > 2*1024*1024 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "图片不能大于2M",
		})
		return
	}
	dst := "./file" + file.Filename
	// 上传文件至指定的完整文件路径
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "成功",
			"data": file,
		})
		return
	}

}
func Delete(c *gin.Context) {
	id := c.Query("id")
	var scenic model.Scenic
	model.DB.Where("id=?", id).Delete(&scenic)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return
}

func List(c *gin.Context) {
	var scenic []model.Scenic
	var total int64
	model.DB.Model(&model.Scenic{}).Count(&total)
	model.DB.Find(&scenic).Debug()
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "成功",
		"data":  scenic,
		"total": total,
	})
	return
}
